use fltk::{
    text::{TextEditor, TextBuffer},
    enums::{Color, FrameType},
    prelude::*
};

use crate::common::event::AppEvent;

// 颜色常量
const COLOR_IVORY: Color = Color::from_rgb(255, 255, 240);  // 象牙白
const COLOR_TEXT: Color = Color::from_rgb(0, 0, 0);  // 文本颜色
const COLOR_WHITE: Color = Color::White;  // 纯白色

pub struct CenterPanel {
    _app_width: i32,    // app width
    _app_height: i32,   // app height
    _start_x: i32,      // position x of the panel
    _start_y: i32,      // position y of the panel
    _width: i32,        // width of the panel
    _height: i32,       // height of the panel
    _editor: TextEditor,
}

impl CenterPanel {
    pub fn new(app_width: i32, app_height: i32, start_x: i32, start_y: i32) -> Self {
        let buffer = TextBuffer::default();
        let (w, h) = Self::draw_area_size(app_width, app_height);
        let mut text_editor = TextEditor::new(start_x, start_y, w, h, "");

        text_editor.set_buffer(buffer);
        text_editor.set_frame(FrameType::DownBox);
        text_editor.set_color(COLOR_WHITE);  // 设置为纯白色
        text_editor.set_text_color(COLOR_TEXT);
        text_editor.set_text_size(12);
        text_editor.redraw();

        Self {
            _app_width:     app_width,
            _app_height:    app_height,
            _start_x:       start_x,
            _start_y:       start_y,
            _width:         w,
            _height:        h,
            _editor:        text_editor,
        }
    }

    fn draw_area_size(app_width: i32, app_height: i32) -> (i32, i32) {
        let w = app_width / 5 * 3;
        let h = app_height / 3 * 2;
        println!("center draw_area_size: {}, {}", w, h);
        (w, h)
    }

    pub fn get_coordinates(&self) -> (i32, i32, i32, i32) {
        (self._start_x, self._start_y, self._start_x + self._width, self._start_y + self._height)
    }

    pub fn handle_event(&mut self, event: &AppEvent) {
        println!("CenterPanel handleEvent {:?}", event);
    }
}
